package com.changgou.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.changgou.canal.config.RabbitMQConfig;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author kfzhao
 * @version 1.0
 * @className BusinessListener
 * @description
 * @date 2021/12/26 22:14
 */
@CanalEventListener
public class BusinessListener {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 注解描述了监听哪个库与哪张表
     *
     * @param eventType 当前操作数据库的类型
     * @param rowData   当前操作数据库的数据
     * @return void
     * @throws
     * @description
     * @author kfzhao
     * @date 2021/12/26 22:15
     */
    @ListenPoint(schema = "changgou_business", table = "tb_ad")
    public void addUpdate(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        System.out.println("广告表数据发生改变");
        // 获取改变之前的数据
        //rowData.getBeforeColumnsList().forEach((c) -> System.out.println("改变前的数据：" + c.getName() + "::" + c.getValue()));

        // 获取改变之后的数据
        //rowData.getAfterColumnsList().forEach((c) -> System.out.println("改变之后的数据：" + c.getName() + "::" + c.getValue()));
        for (CanalEntry.Column column : rowData.getAfterColumnsList()) {
            if ("position".equals(column.getName())) {
                System.out.println("发送最新的数据到消息队列" + column.getValue());
                // 发送消息
                // 利用模板类发送消息
                rabbitTemplate.convertAndSend("", RabbitMQConfig.SEARCH_ADD_QUEUE, column.getValue());
            }
        }
    }
}
